define
(
    ["require",
     "lib/knockout",
     "Common/Validate"],
    function (require, ko, Validate)
    {
        ko.bindingHandlers.contentBind = 
        {
            init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext)
            {                
                var bindingTerm = valueAccessor();
                var contentUrl = ko.utils.unwrapObservable(bindingTerm);

                if (!Validate.notNullOrEmpty(contentUrl))
                {
                    throw "url must be a valid location";
                }                

                if (contentUrl === $(element).data("content"))
                {
                    // Reentrancy check since this binding is called when data is bound upon content load
                    $(element).removeData("content");
                    return;
                }

                var dataContext = ko.dataFor(element);

                $(element).load(contentUrl, function()
                {
                    $(element).data("content", contentUrl);
                    ko.applyBindings(dataContext, element);
                });                
            }
        };
    }
);